﻿/*
 * Author: .net follower (http://dotnetfollower.com)
 * Url: http://bdcrequestlib.codeplex.com/
 * Title: SharePoint Bdc Requests
 * Copyright: � 2012 .net follower 
 * Usage: software is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.BusinessData.Runtime;
using Microsoft.BusinessData.Runtime;

namespace SharePointBdcRequests
{
    /// <summary>
    /// Represents search of data item in external data source by basing on a certain field value. 
    /// The value has to belong to the field used in item _picker in UI
    /// </summary>
    public class BdcRequestByValue : BdcRequestSingle
    {
        /// <summary>
        /// Field value to search data item by
        /// </summary>
        public object RequestedValueToSearch
        {
            get { return ((BdcRequestByValueParams)_bdcRequestParams).ValueToSearch; }
        }

        public string FilterName
        {
            get { return ((BdcRequestByValueParams)_bdcRequestParams).FilterName; }
        }

        /// <summary>
        /// Initializes a new instance of the BDCRequestByValue class
        /// </summary>
        public BdcRequestByValue(BdcRequestByValueParams bdcRequestParams) : base(bdcRequestParams)
        {
            // check if bdcEntityInstanceFieldValue isn't NULL
            EnsureParamIsNotNull("bdcEntityInstanceFieldValue", bdcRequestParams.ValueToSearch);
        }

        /// <summary>
        /// Gets BDC Entity Instance that meets the field value
        /// </summary>
        /// <returns>Found BDC Entity Instance</returns>
        protected override List<IEntityInstance> GetEntityInstances()
        {
            IFilterCollection filters = FoundEntity.GetDefaultFinderFilters();

            IFilter filter = string.IsNullOrEmpty(FilterName) ? 
                filters[0] :
                filters.FirstOrDefault(f => FilterName.Equals(f.FilterDescriptor.Name, StringComparison.OrdinalIgnoreCase) || 
                                            (f.FilterDescriptor.ContainsLocalizedDisplayName() && FilterName.Equals(f.FilterDescriptor.GetLocalizedDisplayName(), 
                                       StringComparison.OrdinalIgnoreCase)));

            if (filter is WildcardFilter)
                ((WildcardFilter)filter).Value = RequestedValueToSearch; //"%" + RequestedValueToSearch + "%";
            else if (filter is ComparisonFilter)
                ((ComparisonFilter)filter).Value = RequestedValueToSearch;

            List<IEntityInstance> res = new List<IEntityInstance>(1);

            using(IEntityInstanceEnumerator enumerator = FoundEntity.FindFiltered(filters, FoundLobSystemInstance))
                if (enumerator.MoveNext())
                    res.Add(enumerator.Current);

            return res;
        }
    }
}
